package com.bravo.friend.controller;

import com.bravo.common.entity.Result;
import com.bravo.common.entity.StatusCode;
import com.bravo.friend.service.FriendService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: Yuyong
 * @create: 2019-08-30 17:37
 **/

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 删除好友
     * @param friendId
     * @return
     */
    @DeleteMapping(value = "/{friendId}")
    public Result remove(@PathVariable String friendId){
        Claims claim = (Claims) request.getAttribute("user_claims");
        if (claim == null) {
            return new Result(false, StatusCode.ACCESSERROR, "无权访问");
        }
        friendService.deleteFriend(claim.getId(),friendId);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 添加好友
     *
     * @param friendId 对方用户ID
     * @param type     1喜欢,0不喜欢
     * @return
     */
    @PutMapping(value = "/like/{friendId}/{type}")
    public Result addFriend(@PathVariable String friendId, @PathVariable String type) {

        Claims claim = (Claims) request.getAttribute("user_claims");
        if (claim == null) {
            return new Result(false, StatusCode.ACCESSERROR, "无权访问");
        }
        //如果是喜欢
        if ("1".equals(type)) {
            if (friendService.addFriend(claim.getId(), friendId) == 0) {
                return new Result(false, StatusCode.REPERROR, "已经添加此好友");
            }
        } else {
            //向不喜欢列表中添加记录
            friendService.addNoFriend(claim.getId(),friendId);
        }
        return new Result(true,StatusCode.OK,"操作成功");
    }


}
